﻿using IOP.ISEA;
using IOP.ISEA.Generator;
using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace VkSample82
{
    class Program
    {
        static async Task Main(string[] args)
        {
            await CreateISEA(true);
            var h = Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) config.AddVulkanJsonFile("Vulkan.json");
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) config.AddVulkanJsonFile("Vulkan-Linux.json");
                    else throw new NotSupportedException("Not supported Platform");
                })
                .ConfigureSgrAHost((sgra) =>
                {
                    sgra.AddVulkanEnvironmental();
                    sgra.UseStartup<Startup>();
                }).ConfigureLogging((logger) => logger.AddConsole()); ;
            await h.RunConsoleAsync();
        }

        static async Task CreateISEA(bool isNeed)
        {
            if (!isNeed) return;
            var basePath = AppContext.BaseDirectory;
            var assetsPath = Path.Combine(basePath, "Assets", "generator.json");
            using var stream = File.OpenRead(assetsPath);
            ISEADocument isea = await ISEAGenerator.GenerateDocument(stream);

            BallData ballData = new BallData();
            SkyData bigSkyData = new SkyData();
            SkyData smallSkyData = new SkyData();
            ballData.GenBallData(3);
            bigSkyData.GenSkyData(400);
            smallSkyData.GenSkyData(1600);

            MeshNode smallSkyMesh = isea.CreateEmptyMeshNode("SmallSkyMesh", (int)smallSkyData.VCount)
                .AddMeshData(smallSkyData.Data, 24, 0, MeshDataPurpose.COMPLEX, true);
            isea.CreateRenderObjectNode("SmallSky", smallSkyMesh);
            MeshNode bigSkyMesh = isea.CreateEmptyMeshNode("BigSkyMesh", (int)bigSkyData.VCount)
                .AddMeshData(bigSkyData.Data, 24, 0, MeshDataPurpose.COMPLEX, true);
            isea.CreateRenderObjectNode("BigSky", bigSkyMesh);

            MeshNode ball = isea.CreateEmptyMeshNode("Ball", (int)ballData.VCount)
                .AddMeshData(ballData.Data, 24, 0, MeshDataPurpose.COMPLEX, true);
            if (isea.TryGetNodeFromName("Earth", out RenderObjectNode earth))
            {
                earth.Comonents.Add(ball);
            }
            if(isea.TryGetNodeFromName("Moon", out RenderObjectNode moon))
            {
                moon.Comonents.Add(ball);
            }
            if(isea.TryGetNodeFromName("Cloud", out RenderObjectNode cloud))
            {
                cloud.Comonents.Add(ball);
            }
            await isea.Save(basePath, "test82");
        }
    }
}
